package com.剑指offer一;

/**
 * @ClassName: Search
 *
 * @Description: TODO( 剑指 Offer 53 - I. 在排序数组中查找数字 I)
 * 统计一个数字在排序数组中出现的次数。
 *
 * @author: li
 * @Date: 2021/12/19 9:13 下午
 */
public class Search {

    public int search(int[] nums, int target) {

        // 找左边界和有边界
        return searchLeft(nums,target+1) - searchLeft(nums,target);

    }

    /**
     * 找左边界
     * @param nums
     * @param target
     * @return
     */
    private int searchLeft(int[] nums,int target){
        int left =0,right = nums.length;
        while (left<right){
            int mid = left+(right-left)/2;

            if (nums[mid] == target){
                right = mid;
            }else if (nums[mid]>target){
                right = mid;
            }else if(nums[mid]<target){
                left = mid+1;
            }
        }
        return left;

    }


    public static void main(String[] args) {
        System.out.println(new Search().search(new int[]{2,3,3,4},4));
    }
}
